import os
import logging
import yaml
from faker import Faker

ANDROID_BASE_CAPS = {
    "appPackage": "com.tencent.wework",
    "appActivity": ".launch.LaunchSplashActivity",
    "automationName": "UIAutomator2",
    "platformName": "Android",
    "platformVersion": "6.0.1",
    "deviceName": "127.0.0.1:7555",
    "noReset": True,
    "newCommandTimeout": "6200",
    "skipDeviceInitialization": True,
    "skipServerInitialization": True,
    "adbExecTimeout": 60000
}

IOS_BASE_CAPS = {
    'app': os.path.abspath('../apps/TestApp.app.zip'),
    'automationName': 'xcuitest',
    'platformName': 'iOS',
    'platformVersion': '12.2',
    'deviceName': 'iPhone 8 Simulator',
    # 'showIOSLog': False,
    "newCommandTimeout": "60",
    "noReset": True,
}

EXECUTOR = 'http://127.0.0.1:4723/wd/hub'


class AppTestLog:
    """
    日志功能
    """
    app_logger = None
    app_logger_stream_handler = None
    app_logger_file_handler = None
    app_logger_formatter = None

    def __init__(self,name,logpath='./log/AppTestLog.log'):
        self.app_logger = logging.getLogger(name)
        self.app_logger.setLevel(logging.DEBUG)
        self.app_logger_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        self.app_logger_stream_handler = logging.StreamHandler()
        self.app_logger_file_handler = logging.FileHandler(logpath, encoding='utf-8')
        self.app_logger_stream_handler.setLevel(logging.DEBUG)
        self.app_logger_file_handler.setLevel(logging.DEBUG)
        self.app_logger_stream_handler.setFormatter(self.app_logger_formatter)
        self.app_logger_file_handler.setFormatter(self.app_logger_formatter)
        self.app_logger.addHandler(self.app_logger_stream_handler)
        self.app_logger.addHandler(self.app_logger_file_handler)

    def log_debug(self, msg):
        self.app_logger.debug(msg)

    def log_info(self, msg):
        self.app_logger.info(msg)

    def log_warning(self, msg):
        self.app_logger.warning(msg)

    def log_error(self, msg):
        self.app_logger.error(msg)


class TestDataBuild:
    """
    生成测试数据文件
    """
    test_data = []
    path = "D:\\PycharmProjects\\HogwartsSDET21\\appium_practice\\PO_MODE\\until\\testdata.yaml"
    log = AppTestLog(__name__,logpath='TestDataBuild.log')

    @classmethod
    def get_name(cls):
        # 生成姓名
        return Faker("zh_CN").name()

    @classmethod
    def get_phone(cls):
        # 生成电话号
        return Faker("zh_CN").phone_number()

    def create_test_data(self, num=10):
        self.test_data = [{'name': TestDataBuild.get_name(), 'phone_num': TestDataBuild.get_phone()} for i in
                          range(num)]
        self.log.log_info(self.test_data)
        with open(self.path, "w", encoding='gbk') as fildobj:
            yaml.safe_dump(self.test_data, fildobj)

    def get_test_data(self,test_data_no=0):
        with open(self.path, "r", encoding='gbk') as fildobj:
            self.test_data = list(yaml.safe_load(fildobj))
            test_data_len = int(len(self.test_data))
            self.log.log_info(test_data_len)
            self.log.log_info(self.test_data)
            self.log.log_info(self.test_data.__getitem__(test_data_no))
            if test_data_no <= test_data_len:
                return self.test_data.__getitem__(test_data_no)
            else:
                raise IndexError

if __name__ == '__main__':
    TestDataBuild().create_test_data()


